All files / web/src/app/api/practice-picker/v1/students/[id]/notes route.ts

86.11% Statements 31/36
60% Branches 3/5
0% Functions 0/1
86.11% Lines 31/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x     1x 1x 1x 1x 1x 1x 2x       2x  
import { NextResponse } from 'next/server'
import { db } from '@/db'
import { withAuth } from '@/lib/auth/withAuth'
import { isParentOf } from '@/lib/classroom/access-control'
import { PRACTICE_PICKER_API_VERSION } from '@/lib/practice-picker/contract'
import { getUserId } from '@/lib/viewer'
 
/** GET /api/practice-picker/v1/students/[id]/notes */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { id } = (await params) as { id: string }
    const userId = await getUserId()
 
    if (!(await isParentOf(userId, id))) {
      return NextResponse.json({ error: 'Student not found or unauthorized' }, { status: 404 })
    }
 
    const student = await db.query.players.findFirst({
      where: (players, { eq }) => eq(players.id, id),
      columns: { id: true, notes: true },
    })
 
    if (!student) {
      return NextResponse.json({ error: 'Student not found' }, { status: 404 })
    }
 
    return NextResponse.json({
      version: PRACTICE_PICKER_API_VERSION,
      studentId: student.id,
      notes: student.notes,
    })
  } catch (error) {
    console.error('Failed to fetch practice picker notes:', error)
    return NextResponse.json({ error: 'Failed to fetch student notes' }, { status: 500 })
  }
})